/*
 * Copyright @2011 Dnbiz. All rights reserved.
 */
package com.dnbiz.api.sdk.util;

/**
 * <pre>
 * 字符串相关操作
 * </pre>
 * @author HuangFeng(2011-1-22)
 */
public class StringUtil {

	/**
	 * 大写字母转成小写字母并在前面加下划线
	 * <code>AdminStr => _admin_str</code>
	 * @author HuangFeng(2011-1-22)
	 * @param string 待转换的字符串
	 * @return 已转换字符串
	 */
	public static String getBeforUpperCaseAddUnderline(String string)
	{
		StringBuilder sb = new StringBuilder();
		for(char c : string.toCharArray())
		{
			if(c >= 65 && c <= 90)
			{
				sb.append("_");
			}
			
			sb.append(String.valueOf(c).toLowerCase());
		}
		
		return sb.toString();
	}
	
	/**
	 * 小写字母下划线的字符串 去除下划线并对下划线后面跟着的小写字母转成大写字母
	 * <code> _admin_str => AdminStr</code>
	 * @author HuangFeng(2011-1-22)
	 * @param string 待转换的字符串
	 * @return 已转换字符串
	 */
	public static String getLowerCaseRemoveUnderline(String string)
	{
		StringBuilder sb = new StringBuilder();
		char[] chars = string.toCharArray();
		for(int i = 0; i < chars.length; i ++)
		{
			if(chars[i] == 95)//_
			{
				if(i < chars.length - 1)
				{
					i ++;
					sb.append(String.valueOf(chars[i]).toUpperCase());
				}
			} else {
				sb.append(String.valueOf(chars[i]));
			}
		}
		
		return sb.toString();
	}
	
	public static void main(String[] args) {
		System.out.println(StringUtil.getLowerCaseRemoveUnderline("q_w_212_andID_"));
		
		/*long start = System.currentTimeMillis();
		int n = 10000000;
		
		for(int i = 0; i < n; i ++)
		{
			
		}
		
		System.out.println((System.currentTimeMillis() - start));*/
	}
}
